package xu.java;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Arrays;
import java.util.Properties;

public class KafkaConsumer {
    public static void main(String[] args) {
        //java 配置项
        //1）bootstrap server, key , value 的反序列化
        //2)创建kafka 消费者组, kafka 的消费者id
        //3)订阅kafka的 topic
        //4)通过循环的形式，打印


        Properties properties = new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.61.128:9092");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.GROUP_ID_CONFIG,"QiGeMingZi");
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");

        KafkaConsumer<String,String> kfkConsumer = new KafkaConsumer<>(properties);

        //topic name

        kfkConsumer.subscribe(Arrays.asList("15homework"));

        while(true){
            ConsumerRecords<String, String> conRecord = kfkConsumer.poll(5);
            for (ConsumerRecord<String,String> consumer : conRecord){
                System.out.println("value is : " + consumer.value());
            }
        }
    }
}
